#include<stdio.h>
#include<stdlib.h>
#define true 1
#define false 0

#define ElementType void* 

struct DynamicArray
{
    ElementType *dp;         //指向数组的指针
    int len;                //数组的长度
    int size;              //总的大小
};

typedef struct DynamicArray DArray;

int Initialize(DArray *array)
{
    array->dp = (ElementType *)malloc(sizeof(ElementType*)*array->size);
    if(array->dp==NULL)
    {
        printf("init error!\n");
        return false;
    }
    return true;    
}

void FreeArray(DArray *array)
{
    if(array->dp!=NULL)
    {
        free(array->dp);
        array->dp=NULL;
        array->size=0;
    }
}

int Realloc(DArray *array)
{
    ElementType *temp = array->dp;
    array->size=array->size*2;
    array->dp = (ElementType *)malloc(sizeof(ElementType *)*array->size);
    if(array->dp==NULL)
    {
        printf("reallc error!\n");
        array->dp=temp;
        array->size = array->size/2;
        return false;
    }
    for(int i=0;i<array->len;i++)
    {
        array->dp[i]=temp[i];
    }
    free(temp);
    temp=NULL;
    return true;
}

int InsertArray(DArray *array,ElementType element)
{
    if(array->len==array->size)
    {
        if(Realloc(array)==false)
        {
            printf("can not contain more\n");
            return false;
        }
    }
    array->dp[array->len]=element;
    array->len++;
    return true;
}

void travel(DArray *array)
{
    for(int i=0;i<array->len;i++)
    {
        printf("%d ",array->dp[i]);
    }
    printf("\n");
}

int main()
{
    struct DynamicArray array={NULL,0,10};
    Initialize(&array);
    for(int i=0;i<10;i++)
    {
        InsertArray(&array,i+1);
    }
    travel(&array);
    for(int i=0;i<10;i++)
    {
        InsertArray(&array,(i+1)*10);
    }
    travel(&array);

    FreeArray(&array);
    return 0;
}
